/*
    Project: OCMF (Operant Chamber Management Framework)
    Version: 1.0
    Author: Copyright (C) 2010, Samuel Delaney & Andrew Harvey
    Licence:
        This file is part of OCMF.

            OCMF is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.

            OCMF is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.

            You should have received a copy of the GNU General Public License
            along with OCMF.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "GoTo.h"
#include "Experiment.h"

GoTo::GoTo(QDomElement gotoElement, Action * parentAction):Action()
{
    trialIndex = gotoElement.attribute("TrialIndex").toUInt(NULL,10);

    if(gotoElement.hasAttribute("Counter")){
        isConditional = true;
        counterName = gotoElement.attribute("Counter");
        inequality = Inequality::Parse(gotoElement.attribute("Inequality"));
        threshold = gotoElement.attribute("Threshold").toInt(NULL,10);
   }
}

void GoTo::Do()
{
    //set the current trial index to the trial before the destination trial (automatically incremented by for loop in the do function of the experiment)
    //((Experiment *)Owner()->Owner()->Owner())->currentActionIndex = trialIndex - 1;

    //end the current image actions - post trial actions will be done before traversing to next trial
    //((ActionCollection*)Owner())->currentActionIndex = ((ActionCollection*)Owner()->Owner()->Owner())->Actions.count();

    //log result
    Action::WriteResult(ToResultXml());
}

QDomElement GoTo::ToResultXml()
{
    QDomElement gotoXmlElement;

    //set tag name
    gotoXmlElement.setTagName("GoTo");

    //set attributes
    gotoXmlElement.setAttribute("TimeStamp", TimeStamp.toString(Qt::ISODate));

    gotoXmlElement.setAttribute("TrialIndex", trialIndex);

    if(isConditional)
    {
        gotoXmlElement.setAttribute("Counter", counterName);
        gotoXmlElement.setAttribute("Inequality", Inequality::ToString(inequality));
        gotoXmlElement.setAttribute("Threshold", threshold);
    }

    //return by value
    return gotoXmlElement;
}
